//
//  YQHomeTableViewController.m
//  WeiboOC
//
//  Created by yang on 15/11/11.
//  Copyright © 2015年 yang. All rights reserved.
//

#import "YQHomeTableViewController.h"
#define HomeCellId @"HomeCellId"
@interface YQHomeTableViewController ()
@property (nonatomic, strong) NSMutableArray* status;
@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
@end
@implementation YQHomeTableViewController
-(NSMutableArray *)status{
    if (!_status) {
        _status = [NSMutableArray array];
    }
   return _status;
}
- (void)viewDidLoad
{
    [super viewDidLoad];

    if (!self.userLogin) {
        [self.loginView setUIInfoImage:nil title:@"关注一些人，回这里看看有什么惊喜"];
        return;
    }
    [self prepareTableView];
    [self loadData];
}
- (void)prepareTableView
{
    self.refreshControl = [[UIRefreshControl alloc]init];

    [self.refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
    [self.tableView registerClass:[YQStatusCell class] forCellReuseIdentifier:HomeCellId];
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.indicatorView  = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.tableView.tableFooterView = self.indicatorView;
}
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.status.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    YQStatusCell* cell = [tableView dequeueReusableCellWithIdentifier:HomeCellId forIndexPath:indexPath];
    cell.status = self.status[indexPath.row];
    if (indexPath.row == self.status.count - 1 && !self.indicatorView.isAnimating) {
        [self.indicatorView startAnimating];
        [self loadData];
        NSLog(@"默认加载");
    }
    return cell;
}
#pragma mark - 加载微博首页数据
- (void)loadData
{
    NSNumber* since_id = 0;
    NSNumber* max_id = 0;
    if (self.indicatorView.isAnimating) {
        since_id = 0;
        max_id = [[self.status lastObject] ID];
    }else{
        since_id = [[self.status firstObject] ID];
        max_id = 0;
    }
    [YQStatusListViewModel loadDataSince_id:since_id max_id:max_id finished:^(NSArray *array) {
        [self.refreshControl endRefreshing];
        if (array.count == 0) {
            if (since_id == 0 ) {
                [SVProgressHUD showErrorWithStatus:AppErrorTip];
            }
            [self.indicatorView stopAnimating];
            return;
        }
        if (since_id > 0) {
            NSIndexSet *index = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, array.count)];
            [self.status insertObjects:array atIndexes:index];
        }else if( max_id > 0){
            [self.status addObjectsFromArray:array];
            [self.indicatorView stopAnimating];
        }else{
            //首次加载
            [self.status addObjectsFromArray:array];
        }
        
        [self.tableView reloadData];
    }];
}
@end
